package com.mjk.manager.amis.core.controller;


import cn.hutool.core.bean.BeanUtil;

import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.manager.amis.core.dto.AmisCrudDTO;
import com.mjk.manager.amis.core.dto.SpringSessionDTO;
import com.mjk.manager.amis.core.model.SpringSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/mamis/plat/session")
@RestController
public class PlatSessionController {
    @Autowired
    BeeDBService beeDBService;

    @GetMapping("/list")
    public BeeResponse getSessionList() {
        final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        BeeQueryWrapper wrapper = new BeeQueryWrapper(new SpringSession());
        wrapper.condition("PRINCIPAL_NAME is not null");
        List<SpringSession> list = beeDBService.findManyByWrapper(wrapper);

        List<SpringSessionDTO> dtoList = list.stream().map(session -> {
            SpringSessionDTO dto = BeanUtil.copyProperties(session, SpringSessionDTO.class);

            dto.setCreationTimeStr(df.format(new Date(session.getCREATION_TIME())));
            dto.setLastAccessTimeStr(df.format(new Date(session.getLAST_ACCESS_TIME())));
            dto.setExpiryTimeStr(df.format(new Date(session.getEXPIRY_TIME())));

            return dto;
        }).collect(Collectors.toList());

        AmisCrudDTO<SpringSessionDTO> data = new AmisCrudDTO<>();
        data.setItems(dtoList);

        // 返回结果集
        return BeeError.BEE_SUC.tranceError(data);
    }
}
